package com.ch.d0312;

import com.ch.util.DBHelper;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "QueryNewsServlet", value = "/QueryNewsServlet")
public class QueryNewsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pageSize = request.getParameter("pageSize");

        String currentPage = request.getParameter("currentPage");

        int size = Integer.valueOf(pageSize);

        int page = Integer.valueOf(currentPage);

        int begin = size*(page-1);

        //说明 : 该案例的sql直接写在servlet中，项目中不允许
        String sql = "select * from hyfc_news";
        
        //查询当前页的数据
        List<Map<String, Object>> data = DBHelper.selectListMap(sql + "limit ? ,? ", begin, size);

        //查询总行数
        long total = DBHelper.count(sql);

        HashMap<String, Object> result = new HashMap<>();

        result.put("data",data);

        result.put("total",total);

        String json = new Gson().toJson(result);

        response.getWriter().append(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
